/*
 * 上海远境文化传媒有限公司 版权所有
 */
package controllers;

import ext.auth.ActiveNav;
import facade.base.CorpFacade;
import helper.GlobalConfig;
import play.Logger;
import play.mvc.After;
import play.mvc.Before;
import play.mvc.Controller;
import util.play.RequestUtility;
import util.play.TimeTracker;

/**
 * Web Base Controller
 */
public class WebBaseController extends Controller {

    @Before
    public static void setupTimeTracker() {
        TimeTracker.startTrack();

        if (GlobalConfig.APPLICATION_URL_SSL) {
            Logger.info("LOG96A31: RequestUtility.getHeaderIgnoreCase(x-forwarded-proto)=" + RequestUtility.getHeaderIgnoreCase("x-forwarded-proto"));

            //if it's not secure, but Heroku has already done the SSL processing then it might actually be secure after all
            if (!request.secure && RequestUtility.getHeaderIgnoreCase("x-forwarded-proto") != null) {
                // X-Forwarded-Proto
                request.secure = "https".equalsIgnoreCase(RequestUtility.getHeaderIgnoreCase("x-forwarded-proto"));
            } else if (CorpFacade.isPrivateInstallVersion()) {
                request.secure = true;
            }
        }

        renderArgs.put("activeNav", getActiveNavValue());
    }

    @After
    public static void printTimeTracker() {
        TimeTracker.printTimeOutTrack();
    }

    private static String getActiveNavValue() {
        if (getActionAnnotation(ActiveNav.class) != null) {
            return getActionAnnotation(ActiveNav.class).value();
        }
        if (getControllerInheritedAnnotation(ActiveNav.class) != null) {
            return getControllerInheritedAnnotation(ActiveNav.class).value();
        }
        return null;
    }

}
